package hlhj.fhp.newslib.network

import android.content.Context
import com.lzy.okgo.OkGo
import com.lzy.okgo.adapter.Call
import com.lzy.okgo.cache.CacheMode
import com.lzy.okgo.model.Response
import com.lzy.okrx.adapter.ObservableBody
import com.lzy.okrx.adapter.ObservableResponse
import com.tenma.ventures.bean.utils.TMSharedPUtil
import hlhj.fhp.newslib.convert.JsonConvert
import hlhj.fhp.newslib.javabean.*
import io.reactivex.android.schedulers.AndroidSchedulers
import rx.Observable
import rx.schedulers.Schedulers
import tmproject.hlhj.fhp.adplayerhelper.ADVBean

/**
 * Created by Administrator on 2018\6\12 0012.
 */
object Apis {
    fun getADVs(): Observable<Response<ADVBean>>? {
      return  OkGo.get<ADVBean>(Urls.Base_URL+Urls.adv)
                .converter(object :JsonConvert<ADVBean>(){})
                .adapt(ObservableResponse())
                .subscribeOn(Schedulers.io())
    }
    /*栏目列表*/
    fun getColumnList(id:Int?): Observable<Response<ColumBean>> {
        return OkGo.post<ColumBean>(Urls.Base_URL+Urls.ALL_COLUMN)
  /*              .cacheKey("colum")
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)*/
                .params("id", id?.toString() ?: "")
                .converter(object :JsonConvert<ColumBean>(){})
                .adapt(ObservableResponse<ColumBean>())
                .subscribeOn(Schedulers.io())
//                .observeOn()
    }
    /*栏目详情*/
    fun getCat(id:Int,page:Int,num:Int?): Observable<Response<CatBean>> {
        val get = OkGo.get<CatBean>(Urls.Base_URL + Urls.CAT)
        return  get
                .params("id",id)
                .params("page",page)
                .params("number", num ?: 0)
                .converter(object :JsonConvert<CatBean>(){})
                .adapt(ObservableResponse<CatBean>())
                .subscribeOn(Schedulers.io())
    }
    /*专题列表*/
    fun getSpecialList(): Observable<Response<SpecialBean>> {
        return OkGo.get<SpecialBean>(Urls.Base_URL+Urls.SPECIAL)
   /*             .cacheKey("special_list")
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)*/
                .converter(object :JsonConvert<SpecialBean>(){})
                .adapt(ObservableResponse<SpecialBean>())
                .subscribeOn(Schedulers.io())
    }
    /*专题详情*/
    fun getSpecialDetail(id:Int): Observable<Response<SpecialDetailBean>>{
        return OkGo.get<SpecialDetailBean>(Urls.Base_URL+Urls.SPECIAL_DETAIL)
//                  .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params("id",id)
                .converter(object :JsonConvert<SpecialDetailBean>(){})
                .adapt(ObservableResponse<SpecialDetailBean>())
                .subscribeOn(Schedulers.io())
    }
    /*获取logo*/
    fun getLogo(): Observable<Response<LogoBean>> {
        return OkGo.get<LogoBean>(Urls.Base_URL+Urls.GET_LOGO)
//                .cacheKey("logo")
//                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .converter(object :JsonConvert<LogoBean>(){})
                .adapt(ObservableResponse<LogoBean>())
                .subscribeOn(Schedulers.io())

    }
    /*详情*/
    fun getInfoDetail(id :Int): Observable<Response<InfoDetailBean>> {
        return OkGo.get<InfoDetailBean>(Urls.Base_URL+Urls.INFO_DETAIL)
//                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params("id",id)
                .converter(object :JsonConvert<InfoDetailBean>(){})
                .adapt(ObservableResponse<InfoDetailBean>())
                .subscribeOn(Schedulers.io())

    }
    /*搜索*/
    fun serach(str:String,page:Int): Observable<Response<CatBean>> {
        return OkGo.post<CatBean>(Urls.Base_URL+Urls.SEARCH)
//                 .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params("keyword",str)
                .params("page",page)
                .converter(object :JsonConvert<CatBean>(){})
                .adapt(ObservableResponse<CatBean>())
                .subscribeOn(Schedulers.io())
    }
    /*自定义菜单*/
    fun edit(uid:Int,default:String,all:String): Observable<Response<BaseBean>> {
        return OkGo.post<BaseBean>(Urls.Base_URL+Urls.EDIT)
//                 .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params("id",uid)
                .params("default",default)
                .params("all",all)
                .converter(object :JsonConvert<BaseBean>(){})
                .adapt(ObservableResponse<BaseBean>())
                .subscribeOn(Schedulers.io())
    }
    /*获取评论*/
    fun getCommentList(id:Int,token: String?): Observable<CommentBean> {
        return OkGo.get<CommentBean>(Urls.Base_URL+Urls.COMMENT_LIST)
//                  .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params("id",id)
                .params("token",token)
                .headers("token",token)
                .converter(object :JsonConvert<CommentBean>(){})
                .adapt(ObservableBody())
                .subscribeOn(Schedulers.io())
    }
/*
    */
/*获取评论*//*

    fun getCommentList(id:Int): Observable<Response<CommentBean>> {
        return OkGo.get<CommentBean>(Urls.Base_URL+Urls.COMMENT_LIST)
                .params("id",id)
                .converter(object :JsonConvert<CommentBean>(){})
                .adapt(ObservableResponse<CommentBean>())
                .subscribeOn(Schedulers.io())
    }
*/
    /*发送评论*/
    fun doComment(aid:Int,uid:Int,content:String?,token:String?,member_code:String?): Observable<SendBean> {
        return OkGo.get<SendBean>(Urls.Base_URL+Urls.SEND_COMMENT)
//                 .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params("aid",aid)
                .params("uid",uid)
                .params("content",content)
                .params("token",token)
//                .params("member_code",member_code)
                .converter(object :JsonConvert<SendBean>(){})
                .adapt(ObservableBody())
                .subscribeOn(Schedulers.io())
    }
    /*判断是否收藏*/
    fun isColl(member_code:String?,app_id:String,article_id:String,context: Context): Observable<String> {
        return OkGo.post<String>(Urls.Base_URL+Urls.COLLOCTION)
                .headers("token",TMSharedPUtil.getTMToken(context))
                .params("member_code", member_code ?: "")
                .params("app_id",app_id)
                .params("article_id",article_id)
                .converter(object :JsonConvert<String>(){})
                .adapt(ObservableBody())
                .subscribeOn(Schedulers.io())
    }

    /*收藏*/
    fun collect(member_code:String?,title:String,app_id:String,article_id:String,token:String
                ,pic:String?,type:Int,extend:String): Observable<CollBasebean> {
       return OkGo.post<CollBasebean>(Urls.Base_URL+Urls.COLLECT)
                .headers("token",token)
                .params("member_code",member_code ?: "")
                .params("title",title)
                .params("app_id",app_id)
                .params("article_id",article_id)
                .params("intro",title)
               .params("tag","")
               .params("extend",extend)
                .params("pic",pic)
               .params("type",type)
                .converter(object :JsonConvert<CollBasebean>(){})
                .adapt(ObservableBody())
                .subscribeOn(Schedulers.io())
    }

    fun addHis(member_code:String?,title:String,app_id:String,article_id:String,token:String
               ,pic:String?,type:Int,extend:String): Observable<CollBasebean>? {
        return OkGo.post<CollBasebean>(Urls.Base_URL+Urls.His)
                .headers("token",token)
                .params("member_code",member_code ?: "")
                .params("title",title)
                .params("app_id",app_id)
                .params("article_id",article_id)
                .params("intro",title)
                .params("tag","")
                .params("extend",extend)
                .params("pic",pic)
                .params("type",type)
                .converter(object :JsonConvert<CollBasebean>(){})
                .adapt(ObservableBody())
                .subscribeOn(Schedulers.io())
    }
    /*取消收藏*/
    fun cancleCollect(star_id:String,token:String): Observable<TMBaseBean> {
        return OkGo.post<TMBaseBean>(Urls.Base_URL+Urls.CANCLE)
                .headers("token",token)
                .params("star_id",star_id)
                .converter(object :JsonConvert<TMBaseBean>(){})
                .adapt(ObservableBody())
                .subscribeOn(Schedulers.io())

    }

    /**
     * 点赞
     */
    fun itsGood(id:Int,token: String?): Observable<BaseBean>? {
        return OkGo.post<BaseBean>(Urls.Base_URL+Urls.DZ)
                .params("id",id)
                .params("token",token)
                .converter(object :JsonConvert<BaseBean>(){})
                .adapt(ObservableBody<BaseBean>())
                .subscribeOn(Schedulers.io())
    }
}